package com.gnkexy.springframework.beans.factory.support;

import cn.hutool.core.util.ClassUtil;
import com.gnkexy.springframework.beans.BeansException;
import com.gnkexy.springframework.beans.factory.FactoryBean;
import com.gnkexy.springframework.beans.factory.config.BeanDefinition;
import com.gnkexy.springframework.beans.factory.config.BeanPostProcessor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements BeanFactory {
    private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<>();

    @Override
    public Object getBean(String name) throws BeansException {
        Object bean = getSingleton(name);
        if (bean != null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition);
    }

    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        Object bean = getSingleton(name);
        if (bean != null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition, args);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) {
        return null;
    }


    protected <T> T doGetBean(final String name, final Object[] args) throws BeansException {
        Object sharedInstance = getSingleton(name);
        if (sharedInstance != null) {
            // 如果是 FactoryBean，则需要调用 FactoryBean#getObject
            return (T) getObjectForBeanInstance(sharedInstance, name);
        }

        BeanDefinition beanDefinition = getBeanDefinition(name);
        Object bean = createBean(name, beanDefinition, args);
        return (T) getObjectForBeanInstance(bean, name);
    }

    private Object getObjectForBeanInstance(Object beanInstance, String beanName) throws BeansException {
        if (!(beanInstance instanceof FactoryBean)) {
            return beanInstance;
        }

        Object object = getCachedObjectForFactoryBean(beanName);

        if (object == null) {
            FactoryBean<?> factoryBean = (FactoryBean<?>) beanInstance;
            object = getObjectFromFactoryBean(factoryBean, beanName);
        }

        return object;
    }


    public List<BeanPostProcessor> getBeanPostProcessors() {
        List<BeanPostProcessor> postProcessors = getSingletonObjectsByType();
        if (postProcessors != null) {
            return postProcessors;
        }
        return null;
    }

    public List<BeanPostProcessor> getSingletonObjectsByType() {
        List<BeanPostProcessor> list = new ArrayList<>();
        for (Map.Entry<String, Object> stringObjectEntry : getSingletonObjects().entrySet()) {
            Object value = stringObjectEntry.getValue();
            if (value instanceof BeanPostProcessor) {
                BeanPostProcessor postProcessor = (BeanPostProcessor) value;
                list.add(postProcessor);
            }
        }
        return list;
    }


    public ClassLoader classLoader = ClassUtil.getClassLoader();

    public ClassLoader getClassLoader() {
        return this.classLoader;
    }


    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException;


}
